home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / bash_114.zip / bash-1.14.2 / builtins / source.def < prev    next >
C/C++ Source or Header  |  1994-06-26  |  5KB  |  179 lines

  1. This file is source.def, from which is created source.c.
  2. It implements the builtins "." and  "source" in Bash.
  3.  
  4. Copyright (C) 1987, 1989, 1991 Free Software Foundation, Inc.
  5.  
  6. This file is part of GNU Bash, the Bourne Again SHell.
  7.  
  8. Bash is free software; you can redistribute it and/or modify it under
  9. the terms of the GNU General Public License as published by the Free
  10. Software Foundation; either version 1, or (at your option) any later
  11. version.
  12.  
  13. Bash is distributed in the hope that it will be useful, but WITHOUT ANY
  14. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  15. FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  16. for more details.
  17.  
  18. You should have received a copy of the GNU General Public License along
  19. with Bash; see the file COPYING.  If not, write to the Free Software
  20. Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  21.  
  22. $PRODUCES source.c
  23.  
  24. $BUILTIN source
  25. $FUNCTION source_builtin
  26. $SHORT_DOC source filename
  27. Read and execute commands from FILENAME and return.  The pathnames
  28. in $PATH are used to find the directory containing FILENAME.
  29. $END
  30. $BUILTIN .
  31. $DOCNAME dot
  32. $FUNCTION source_builtin
  33. $SHORT_DOC . [filename]
  34. Read and execute commands from FILENAME and return.  The pathnames
  35. in $PATH are used to find the directory containing FILENAME.
  36. $END
  37. /* source.c - Implements the `.' and `source' builtins. */
  38.  
  39. #include <sys/types.h>
  40. #include <sys/file.h>
  41. #include <errno.h>
  42.  
  43. #if defined (HAVE_STRING_H)
  44. #  include <string.h>
  45. #else /* !HAVE_STRING_H */
  46. #  include <strings.h>
  47. #endif /* !HAVE_STRING_H */
  48.  
  49. #include "../shell.h"
  50. #include "../posixstat.h"
  51. #include "../filecntl.h"
  52. #include "../execute_cmd.h"
  53.  
  54. /* Not all systems declare ERRNO in errno.h... and some systems #define it! */
  55. #if !defined (errno)
  56. extern int errno;
  57. #endif /* !errno */
  58.  
  59. /* Variables used here but defined in other files. */
  60. extern int return_catch_flag, return_catch_value;
  61. extern jmp_buf return_catch;
  62. extern int posixly_correct;
  63. extern int interactive, interactive_shell;
  64.  
  65. /* How many `levels' of sourced files we have. */
  66. int sourcelevel = 0;
  67.  
  68. /* If this . script is supplied arguments, we save the dollar vars and
  69.    replace them with the script arguments for the duration of the script's
  70.    execution.  If the script does not change the dollar vars, we restore
  71.    what we saved.  If the dollar vars are changed in the script, we leave
  72.    the new values alone and free the saved values. */
  73. static void
  74. maybe_pop_dollar_vars ()
  75. {
  76.   if (dollar_vars_changed ())
  77.     {
  78.       dispose_saved_dollar_vars ();
  79.       set_dollar_vars_unchanged ();
  80.     }
  81.   else
  82.     pop_dollar_vars ();
  83. }
  84.  
  85. /* Read and execute commands from the file passed as argument.  Guess what.
  86.    This cannot be done in a subshell, since things like variable assignments
  87.    take place in there.  So, I open the file, place it into a large string,
  88.    close the file, and then execute the string. */
  89. source_builtin (list)
  90.      WORD_LIST *list;
  91. {
  92.   int result, return_val;
  93.  
  94.   /* Assume the best. */
  95.   result = EXECUTION_SUCCESS;
  96.  
  97.   if (list)
  98.     {
  99.       char *string, *filename;
  100.       struct stat finfo;
  101.       int fd, tt;
  102.  
  103.       filename = find_path_file (list->word->word);
  104.       if (!filename)
  105.     filename = savestring (list->word->word);
  106.  
  107.       if (((fd = open (filename, O_RDONLY)) < 0) || (fstat (fd, &finfo) < 0))
  108.     goto file_error_exit;
  109.  
  110.       string = (char *)xmalloc (1 + (int)finfo.st_size);
  111.       tt = read (fd, string, finfo.st_size);
  112.       string[finfo.st_size] = '\0';
  113.  
  114.       /* Close the open file, preserving the state of errno. */
  115.       { int temp = errno; close (fd); errno = temp; }
  116.  
  117.       if (tt != finfo.st_size)
  118.     {
  119.       free (string);
  120.  
  121.     file_error_exit:
  122.       file_error (filename);
  123.       free (filename);
  124.  
  125.       /* POSIX shells exit if non-interactive and file error. */
  126.       if (posixly_correct && !interactive_shell)
  127.         longjmp (top_level, EXITPROG);
  128.  
  129.       return (EXECUTION_FAILURE);
  130.     }
  131.  
  132.       if (tt > 80)
  133.     tt = 80;
  134.  
  135.       if (check_binary_file ((unsigned char *)string, tt))
  136.     {
  137.       free (string);
  138.       builtin_error ("%s: cannot execute binary file", filename);
  139.       free (filename);
  140.       return (EX_BINARY_FILE);
  141.     }
  142.  
  143.       begin_unwind_frame ("File Sourcing");
  144.  
  145.       if (list->next)
  146.     {
  147.       push_dollar_vars ();
  148.       add_unwind_protect ((Function *)maybe_pop_dollar_vars, (char *)NULL);
  149.       remember_args (list->next, 1);
  150.     }
  151.  
  152.       unwind_protect_int (return_catch_flag);
  153.       unwind_protect_jmp_buf (return_catch);
  154.       unwind_protect_int (interactive);
  155.       unwind_protect_int (sourcelevel);
  156.       add_unwind_protect ((Function *)xfree, filename);
  157.       interactive = 0;
  158.       sourcelevel++;
  159.  
  160.       set_dollar_vars_unchanged ();
  161.  
  162.       return_catch_flag++;
  163.       return_val = setjmp (return_catch);
  164.  
  165.       if (return_val)
  166.     parse_and_execute_cleanup ();
  167.       else
  168.     result = parse_and_execute (string, filename, -1);
  169.  
  170.       run_unwind_frame ("File Sourcing");
  171.  
  172.       /* If RETURN_VAL is non-zero, then we return the value given
  173.      to return_builtin (), since that is how we got here. */
  174.       if (return_val)
  175.     result = return_catch_value;
  176.     }
  177.   return (result);
  178. }
  179.